//
//  LSSettingVController.m
//  LookScore
//
//  Created by wr on 2017/12/7.
//  Copyright © 2017年 ChongQingXunTiCompany. All rights reserved.
//

#define cellIndex 2
#import "LSSettingVController.h"
#import "LSMineListCell.h"
#import "LSAccountArchive.h"
#import "LSClearCacheTool.h"
#import "MBProgressHUD+MJ.h"
@interface LSSettingVController ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic)UITableView *tableView;

@property(strong,nonatomic)NSArray *imgArray;
@end

@implementation LSSettingVController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
    
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBar.translucent = YES;
    
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor =LSBackGroundColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"设置";
    self.view.backgroundColor=LSBackGroundColor;

    self.imgArray=@[@"清除缓存",@"联系我们"];
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout.leftEqualToView(self.view).topEqualToView(self.view).rightEqualToView(self.view).heightIs(LSControlHeight*cellIndex);

    UIButton *quitBu=[UIButton new];
    quitBu.titleLabel.font=LSFontSize15;
    [quitBu setTitle:@"退出" forState:UIControlStateNormal];
    quitBu.layer.cornerRadius=5;
    quitBu.layer.masksToBounds=YES;
    
    [quitBu setTitleColor:LSTitleWhiteColor forState:UIControlStateNormal];
    [quitBu setBackgroundImage:[CommonClass getImageWithBgColor:LSSubColorDF1C1C] forState:UIControlStateNormal];
//    [loginBu setBackgroundImage:[CommonClass getImageWithBgColor:LSSubColor7ec5fd] forState:UIControlStateHighlighted];
    quitBu.contentVerticalAlignment=UIControlContentHorizontalAlignmentCenter;
    [quitBu addTarget:self action:@selector(quitLoginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:quitBu];
    quitBu.sd_layout.leftEqualToView(self.view).offset(LSSpaceLeftWidth2).topSpaceToView(self.tableView, 29).rightEqualToView(self.view).offset(-LSSpaceLeftWidth2).heightIs(LSButtonHeight);
    
    if (![LSAccountArchive isLogin]) {
        quitBu.hidden = YES;
    }else{
        quitBu.hidden = NO;
    }
    
    
    
    
}
#pragma mark 🔥 tableViewDelegate tableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.imgArray.count;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return LSControlHeight;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LSMineListCell *cell=[LSMineListCell mineListTableViewCellByTableView:tableView];
   
    cell.titleLab.text=self.imgArray[indexPath.row];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
        if (indexPath.row==0) {
            
            // 1.计算缓存大小
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            NSString *cachesDir = [paths objectAtIndex:0];
            float size = [LSClearCacheTool folderSizeAtPath:cachesDir];
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"清除缓存"
                                                            message:[NSString stringWithFormat:@"已清理%.2lfM缓存", size]
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"确定",nil];
            
            [alert show];
            
            [LSClearCacheTool clearCache:cachesDir];
            
            
        }else if (indexPath.row==1){
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt:023-68888888"]];
        }

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    MyLog(@"clickButtonAtIndex:%d",(int)buttonIndex);
    //index为-1则是取消，
    if (buttonIndex==1) {
        [MBProgressHUD showSuccess:@"已清除"];
    }
}

#pragma mark 🔥  事件
//退出
-(void)quitLoginAction:(UIButton *)sender{
    [LSAccountArchive autoLoginSuccessStatus:NO];
    [LSAccountArchive logout];
    [LSAccountArchive saveUserToken:@""];
    [notifiCenter postNotificationName:@"LOGIONOUT" object:self userInfo:nil];
    if (self.logOutSuccessBlock) {
        self.logOutSuccessBlock();
    }
//    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
